<?php

namespace App\Http\Controllers\Reception\Article;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Http\Request;

//文章评论模块
class ArticleCommentController extends Controller
{
    //获取文章评论列表
    public function getCommentList(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifytokens($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['article_comment.aid'] = (int) $request->input('aid');
        $page = (int) $request->query('page');
        $pageSize = (int) $request->query('pageSize');
        //翻页
        $page = $page - 1;
        if ($page != 0) {
            $page = $pageSize * $page;
            $pageSize = $pageSize * $page;
        }
        if (!$map['article_comment.aid']) {
            $data = fail();
            $data['msg'] = '参数错误';
            return $data;
        }
        $map['article_comment.isshow'] = 1;
        $info = DB::table('article_comment')
            ->where($map)
            ->leftjoin('user', 'article_comment.user_id', '=', 'user.user_id')
            ->orderByDesc("comment_create_time")
            ->offset($page)
            ->limit($pageSize)
            ->get();
        if ($info) {
            $info = json_decode($info, true);
            foreach ($info as $key => $value) {
                $info[$key]['nickName'] = urldecode($value['nickName']);
                $info[$key]['comment_create_time'] = date("Y-m-d H:i", $value['comment_create_time']);
            }
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }
    //获取个人评论文章列表
    public function getUserComment(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifytokens($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $page = (int) $request->query('page');
        $pageSize = (int) $request->query('pageSize');
        //翻页
        $page = $page - 1;
        if ($page != 0) {
            $page = $pageSize * $page;
            $pageSize = $pageSize * $page;
        }
        $map['article_comment.user_id'] = (int) $request->query('user_id');

        $info = DB::table('article_comment')
            ->leftJoin('article', 'article_comment.aid', '=', 'article.aid')
            ->leftJoin('upload_file', 'article.fileid', '=', 'upload_file.fileid')
            ->orderByDesc("comment_create_time")
            ->offset($page)
            ->limit($pageSize)
            ->get();
        if ($info) {
            $info = json_decode($info, true);
            $drInfo = array_unset_dr($info, 'aid');
            foreach ($drInfo as $key => $value) {
                // $info[$key]['nickName']=urldecode($value['nickName']);
                $drInfo[$key]['comment_create_time'] = date("Y-m-d H:i", $value['comment_create_time']);
                $drInfo[$key]['url'] =  http_type() . $_SERVER['HTTP_HOST'] . "/" . $value['url'] . "/" . $value['file_name'];
            }
            $data = success();
            $data['data'] = $drInfo;
        } else {
            $data = fail();
        }
        return $data;
    }
    //广场评论列表
    public function getAllComment(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifytokens($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $page = (int) $request->query('page');
        $pageSize = (int) $request->query('pageSize');
        //翻页
        $page = $page - 1;
        if ($page != 0) {
            $page = $pageSize * $page;
            $pageSize = $pageSize * $page;
        }
        $map['article_comment.isshow'] = 1;
        $info = DB::table('article_comment')
            ->where($map)
            ->leftjoin('user', 'article_comment.user_id', '=', 'user.user_id')
            ->leftJoin('article', 'article_comment.aid', '=', 'article.aid')
            ->leftJoin('upload_file', 'article.fileid', '=', 'upload_file.fileid')
            ->orderByDesc("comment_create_time")
            ->offset($page)
            ->limit($pageSize)
            ->get();
        if ($info) {
            $info = json_decode($info, true);
            foreach ($info as $key => $value) {
                $info[$key]['nickName'] = urldecode($value['nickName']);
                $info[$key]['comment_create_time'] = date("Y-m-d H:i", $value['comment_create_time']);
                $info[$key]['url'] =  http_type() . $_SERVER['HTTP_HOST'] . "/" . $value['url'] . "/" . $value['file_name'];
            }
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }
    //文章评论
    public function inComment(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifytokens($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $date['aid'] = (int) $request->input('aid');
        $date['user_id'] = (int) $request->input('user_id');
        if (Cache::has($date['aid'] . '_' . $date['user_id'])) {
            $data=fail();
            $data['msg']='请勿频繁操作';
            return $data;
        }        
        Cache::put($date['aid'] . '_' . $date['user_id'], 1, 300);
        if (!$date['user_id'] || !$date['aid']) {
            $data = fail();
            $data['msg'] = '参数错误';
            return $data;
        }
        $date['parentuser_id'] = (int) $request->input('parentuser_id');
        if (is_string_regular($request->input('comment_content'))) {
            $date['comment_content'] = replaceSpecialChar($request->input('comment_content'));
        } else {
            $date['comment_content'] = $request->input('comment_content');
        }
        $date['ip']=get_real_ip();
        $date['comment_create_time'] = time();

        $msg = DB::table('article_comment')->insert($date);
        if ($msg) {
            $map['aid'] = $date['aid'];
            $msg = DB::table('article')->where($map)->increment('article_comment_num', 1);
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }
}
